#include "StdAfx.h"
#include "GtdDOMItem.h"
#include "cpputil.h"
#include "GtdDOM.h"
#include <algorithm>
	
static TCHAR A_GUID[] = _T("guid");
static TCHAR A_NAME[] =_T("name");
 static TCHAR A_PROJECT_REF[] =_T("project_ref");
 static TCHAR A_TASKENTRY_REF[] =_T("taskentry_ref");
 static TCHAR A_FROM_TIME[] =_T("from_time");
 static TCHAR A_TO_TIME[] =_T("to_time");

 static TCHAR N_TASK_CONTEXT_REFLIST[] = _T("context_ref_list"); 
 static TCHAR N_TASK_CONTEXT_REF[] =_T("context_ref");
 static TCHAR N_TASK_CHEKCITMELIST[] =_T("checkitem_list");
 static TCHAR N_TASK_CHECKITEM[] =_T("checkitem");



//dom item




void GtdDOMItem::saveToXMLNode( xmlNode& node )
{
	node.addAttribute(A_GUID,m_id);
	node.addAttribute(A_NAME,msName);
	toXMLNode(node);
}

bool GtdDOMItem::readFromXMLNode( xmlNode& node )
{
	node.getAttribute(A_GUID,m_id);
	node.getAttribute(A_NAME,msName);
	fromXMLNode(node);

	return true;
}



GtdDOMItem::GtdDOMItem( GtdDOM* pDom )
	:mpDom(pDom)
	,m_id(CGuid::Create())
{

}

//dom project
void GtdProject::toXMLNode( xmlNode& node )
{
	node.addAttribute(A_PROJECT_REF,mParent);
}

void GtdProject::fromXMLNode( xmlNode& node )
{
	node.getAttribute(A_PROJECT_REF,mParent);	
}

GtdProject* GtdProject::GetParent() const
{
	if(GtdDOMItem* pItem = mpDom->getItem(mParent))
	{
		if(GtdProject* pProject = pItem->toProject()) 
		{
			return pProject;
		}
	}
	return NULL;
}



///task entry
void GtdTaskEntry::toXMLNode( xmlNode& node )
{
	node.addAttribute(A_PROJECT_REF,mPrj);
	node.addAttribute(A_FROM_TIME,mtFrom);
	node.addAttribute(A_TO_TIME,mtTo);
	//add context list 
	xmlNode pchild = node.createChild(N_TASK_CHEKCITMELIST);
	for(CheckItemList::const_iterator itr = mCheckList.begin();itr!=mCheckList.end();++itr)
	{
		//itr
		xmlNode pitemnode = pchild.createChild(N_TASK_CHECKITEM);
		//
		TaskEntryCheckItem* pChekItem = *itr;
		pChekItem->saveToXMLNode(pitemnode);		
	}
	//context list
	//xmlNode 
}

void GtdTaskEntry::fromXMLNode( xmlNode& node )
{
	node.getAttribute(A_PROJECT_REF,mPrj);
	node.getAttribute(A_FROM_TIME,mtFrom);
	node.getAttribute(A_TO_TIME,mtTo);
	//read 
	xmlNode listNode = node.firstChild(N_TASK_CHEKCITMELIST);
	if(listNode)
	{
		xmlNode itemnode = listNode.firstChild(N_TASK_CHECKITEM);
		while(itemnode)
		{
			TaskEntryCheckItem* pItem = mpDom->allocateDOMItem<TaskEntryCheckItem>();
			pItem->readFromXMLNode(itemnode);
			AddCheckItem(pItem);

			itemnode = itemnode.nextSibling(N_TASK_CHECKITEM);
		}

	}
}

bool GtdTaskEntry::hasContext( const CGuid& id ) const
{
	return std::find(mCtxlist.begin(),mCtxlist.end(),id)!=mCtxlist.end();
}

bool GtdTaskEntry::IsContexted() const
{
	GuidList ctxlist = mpDom->GetContextList();
	for(GuidList::const_iterator itr = ctxlist.begin();itr!=ctxlist.end();++itr)
	{
		if(hasContext(*itr))
		{
			return true;
		}
	}
	return false;
}



//task item
void GtdTaskItem::toXMLNode( xmlNode& node )
{
	node.addAttribute(A_PROJECT_REF,mTaskEntry);
	node.addAttribute(A_FROM_TIME,mtFrom);
	node.addAttribute(A_TO_TIME,mtTo);
}

void GtdTaskItem::fromXMLNode( xmlNode& node )
{
	node.getAttribute(A_PROJECT_REF,mTaskEntry);
	node.getAttribute(A_FROM_TIME,mtFrom);
	node.getAttribute(A_TO_TIME,mtTo);
}

GtdTaskEntry* GtdTaskItem::GetEntry() const
{
	GtdDOMItem* pitem = mpDom->getItem(mTaskEntry);
	if(pitem)
	{
		return pitem->toTaskEntry();
	}
	return NULL;
}
